package com.lee.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lee.pojo.User;

@RestController
@RequestMapping("/user")
public class UserController {

	static Map<Long,User> users = Collections.synchronizedMap(new HashMap<Long,User>());
	
	@ApiOperation(value="获取用户列表", notes="")
	@GetMapping("")
	public List<User> getUserList(){
		List<User> list = new ArrayList<User>(users.values());
		return list;
	}
	
	@ApiOperation(value="创建用户", notes="根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
	@PostMapping("")
	public String postUser(@RequestBody User user){
		users.put(user.getUserId(), user);
		return "success";
	}
	
	
	@ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "Long",paramType="path")
	@GetMapping("/{userId}")
    public User getUser(@PathVariable Long userId) {
        return users.get(userId);
    }
	
	@ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象，并根据传过来的user信息来更新用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "Long",paramType="path"),
            @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    })
	@PutMapping("/{userId}")
    public String putUser(@PathVariable Long userId, @RequestBody User user) {
        User u = users.get(userId);
        u.setUserName(user.getUserName());
        u.setAddress(user.getAddress());
        users.put(userId, u);
        return "success";
    }
	
	@ApiOperation(value="删除用户", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "Long",paramType="path")
	@DeleteMapping("/{userId}")
    public String deleteUser(@PathVariable Long userId) {
        users.remove(userId);
        return "success";
    }
}
